﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace MyPublisher.Storage
{
    public class StoragePropertyConfiguration : ConfigurationElement
    {
        [ConfigurationProperty("name")]
        public string Name
        {
            get { return (string)this["name"]; }
            set { this["name"] = value; }
        }

        [ConfigurationProperty("value")]
        public string Value
        {
            get { return (string)this["value"]; }
            set { this["value"] = value; }
        }

        public StoragePropertyConfiguration() { }
    }

    public class StoragePropertiesConfiguration : ConfigurationElementCollection
    {
        public StoragePropertiesConfiguration()
        {
        }

        protected override ConfigurationElement CreateNewElement()
        {
            return new StoragePropertyConfiguration();
        }

        protected override Object GetElementKey(ConfigurationElement element)
        {
            return ((StoragePropertyConfiguration)element).Name;
        }

        public StoragePropertyConfiguration this[int index]
        {
            get { return (StoragePropertyConfiguration)BaseGet(index); }
            set
            {
                if (BaseGet(index) != null)
                {
                    BaseRemoveAt(index);
                }
                BaseAdd(index, value);
            }
        }
    } 

    public class StorageConfiguration : ConfigurationElement
    {
        [ConfigurationProperty("readonly")]
        public string Readonly
        {
            get { return (string)this["readonly"]; }
            set { this["readonly"] = value; }
        }

        [ConfigurationProperty("type")]
        public string Type
        {
            get { return (string)this["type"]; }
            set { this["type"] = value; }
        }

        [ConfigurationProperty("name")]
        public string Name
        {
            get { return (string)this["name"]; }
            set { this["name"] = value; }
        }

        [ConfigurationProperty("properties")]
        [ConfigurationCollection(typeof(StoragePropertiesConfiguration), AddItemName = "property")]
        public StoragePropertiesConfiguration StorageProperties
        {
            get { return (StoragePropertiesConfiguration)this["properties"]; }
            set { this["properties"] = value; }
        }

        public Dictionary<string, string> GetProperties()
        {
            Dictionary<string, string> properties = new Dictionary<string, string>();
            if (this.Properties != null)
            {
                foreach (StoragePropertyConfiguration propertyConfig in StorageProperties)
                {
                    properties[propertyConfig.Name] = propertyConfig.Value;
                }
            }
            return properties;
        }

        public StorageConfiguration() { }
    }

    public class StoragesConfiguration : ConfigurationElementCollection
    {
        public StoragesConfiguration()
        {
        }

        protected override ConfigurationElement CreateNewElement()
        {
            return new StorageConfiguration();
        }

        protected override Object GetElementKey(ConfigurationElement element)
        {
            return ((StorageConfiguration)element).Name;
        }

        public StorageConfiguration this[int index]
        {
            get { return (StorageConfiguration)BaseGet(index); }
            set
            {
                if (BaseGet(index) != null)
                {
                    BaseRemoveAt(index);
                }
                BaseAdd(index, value);
            }
        }

        public StorageConfiguration this[string name]
        {
            get { return (StorageConfiguration)BaseGet(name); }
            set
            {
                ConfigurationElement element = BaseGet(name);
                int index = -1;
                if (element != null)
                {
                    index = BaseIndexOf(element);
                    BaseRemove(name);
                }
                if (index != -1)
                {
                    BaseAdd(index, value);
                }
                else
                {
                    BaseAdd(value);
                }
            }
        }
    }

    public class StorageConfigurationSection : ConfigurationSection
    {
        [ConfigurationProperty("storages", IsDefaultCollection = false)]
        [ConfigurationCollection(typeof(StoragesConfiguration), AddItemName = "storage")]
        public StoragesConfiguration Storages
        {
            get
            {
                return (StoragesConfiguration)this["storages"];
            }
            set
            {
                this["storages"] = value;
            }
        }
    }
}
